bleh
[mqlkit.git] / mGRID EA.mq4
blob7cc4b045f4c09851202e34fbef72879c6cdcac69
1 //---- input parameters ---------------------------------------------+\r
2 \r
3 extern int       INCREMENT=35;\r
4 extern double    LOTS=0.1;\r
5 extern int       LEVELS=3; \r
6 extern double    MAX_LOTS=99;\r
7 extern int       MAGIC=1803;\r
8 extern bool      CONTINUE=true;\r
9 extern bool      MONEY_MANAGEMENT=false;\r
10 extern int       RISK_RATIO=2;\r
11 //+------------------------------------------------------------------+\r
13 bool             UseProfitTarget=false;\r
14 bool             UsePartialProfitTarget=false;\r
15 int              Target_Increment = 50;\r
16 int              First_Target = 20;\r
17 bool             UseEntryTime=false;\r
18 int              EntryTime=0;\r
20 //+------------------------------------------------------------------+\r
22 bool Enter=true;\r
23 int nextTP;\r
25 int init()\r
26   {\r
27 //+------------------------------------------------------------------+ \r
28    nextTP = First_Target;\r
29 //+------------------------------------------------------------------+\r
30    return(0);\r
31   }\r
32 //+------------------------------------------------------------------+\r
33 //| expert deinitialization function                                 |\r
34 //+------------------------------------------------------------------+\r
35 int deinit()\r
36   {\r
37    return(0);\r
38   }\r
39 //+------------------------------------------------------------------+\r
40 //| expert start function                                            |\r
41 //+------------------------------------------------------------------+\r
42 int start()\r
43   {\r
44    int ticket, cpt, profit, total=0, BuyGoalProfit, SellGoalProfit, PipsLot;\r
45    double ProfitTarget=INCREMENT*2, BuyGoal=0, SellGoal=0, spread=(Ask-Bid)/Point, InitialPrice=0;\r
46 //----\r
47   \r
48    if(INCREMENT<MarketInfo(Symbol(),MODE_STOPLEVEL)+spread) INCREMENT=1+MarketInfo(Symbol(),MODE_STOPLEVEL)+spread;\r
49    if(MONEY_MANAGEMENT) LOTS=NormalizeDouble(AccountBalance()*AccountLeverage()/1000000*RISK_RATIO,0)*MarketInfo(Symbol(),MODE_MINLOT);\r
50    if(LOTS<MarketInfo(Symbol(),MODE_MINLOT))\r
51    {\r
52       Comment("Not Enough Free Margin to begin");\r
53       return(0);\r
54    }\r
55    for(cpt=1;cpt<LEVELS;cpt++) PipsLot+=cpt*INCREMENT;\r
56    for(cpt=0;cpt<OrdersTotal();cpt++)\r
57    {\r
58       OrderSelect(cpt,SELECT_BY_POS,MODE_TRADES);\r
59       if(OrderMagicNumber()==MAGIC && OrderSymbol()==Symbol())\r
60       {\r
61          total++;\r
62          if(!InitialPrice) InitialPrice=StrToDouble(OrderComment());\r
63          if(UsePartialProfitTarget && UseProfitTarget && OrderType()<2)\r
64          {\r
65             double val=getPipValue(OrderOpenPrice(),OrderType());\r
66             takeProfit(val,OrderTicket()); \r
67          }\r
68       }\r
69    }\r
70    if(total<1 && Enter && (!UseEntryTime || (UseEntryTime && Hour()==EntryTime)))\r
71    {\r
72       if(AccountFreeMargin()<(100*LOTS))\r
73       {\r
74          Print("Not enough free margin to begin");\r
75          return(0);\r
76       }\r
77       // - Open Check - Start Cycle\r
78       InitialPrice=Ask;\r
79       SellGoal=InitialPrice-(LEVELS+1)*INCREMENT*Point;\r
80       BuyGoal=InitialPrice+(LEVELS+1)*INCREMENT*Point;\r
81       for(cpt=1;cpt<=LEVELS;cpt++)\r
82       {\r
83          OrderSend(Symbol(),OP_BUYSTOP,LOTS,InitialPrice+cpt*INCREMENT*Point,2,SellGoal,BuyGoal,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),MAGIC,0);\r
84          OrderSend(Symbol(),OP_SELLSTOP,LOTS,InitialPrice-cpt*INCREMENT*Point,2,BuyGoal+spread*Point,SellGoal+spread*Point,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),MAGIC,0);\r
85       }\r
86     } // initial setup done - all channels are set up\r
87     else // We have open Orders\r
88     {\r
89       BuyGoal=InitialPrice+INCREMENT*(LEVELS+1)*Point;\r
90       SellGoal=InitialPrice-INCREMENT*(LEVELS+1)*Point;\r
91       total=OrdersHistoryTotal();\r
92       for(cpt=0;cpt<total;cpt++)\r
93       {\r
94          OrderSelect(cpt,SELECT_BY_POS,MODE_HISTORY);\r
95          if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC &&  StrToDouble(OrderComment())==InitialPrice){EndSession();return(0);}\r
96       }\r
97       if(UseProfitTarget && CheckProfits(LOTS,OP_SELL,true,InitialPrice)>ProfitTarget) {EndSession();return(0);}\r
98       BuyGoalProfit=CheckProfits(LOTS,OP_BUY,false,InitialPrice);\r
99       SellGoalProfit=CheckProfits(LOTS,OP_SELL,false,InitialPrice);\r
100       if(BuyGoalProfit<ProfitTarget)\r
101       // - Incriment Lots Buy\r
102       {\r
103          for(cpt=LEVELS;cpt>=1 && BuyGoalProfit<ProfitTarget;cpt--)\r
104          {\r
105             if(Ask<=(InitialPrice+(cpt*INCREMENT-MarketInfo(Symbol(),MODE_STOPLEVEL))*Point))\r
106             {\r
107                ticket=OrderSend(Symbol(),OP_BUYSTOP,cpt*LOTS,InitialPrice+cpt*INCREMENT*Point,2,SellGoal,BuyGoal,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),MAGIC,0);\r
108             }\r
109             if(ticket>0) BuyGoalProfit+=LOTS*(BuyGoal-InitialPrice-cpt*INCREMENT*Point)/Point;\r
110          }\r
111       }\r
112       if(SellGoalProfit<ProfitTarget)\r
113       // - Increment Lots Sell\r
114       {\r
115          for(cpt=LEVELS;cpt>=1 && SellGoalProfit<ProfitTarget;cpt--)\r
116          {\r
117             if(Bid>=(InitialPrice-(cpt*INCREMENT-MarketInfo(Symbol(),MODE_STOPLEVEL))*Point))\r
118             {\r
119                ticket=OrderSend(Symbol(),OP_SELLSTOP,cpt*LOTS,InitialPrice-cpt*INCREMENT*Point,2,BuyGoal+spread*Point,SellGoal+spread*Point,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),MAGIC,0);\r
120             }\r
121             if(ticket>0) SellGoalProfit+=LOTS*(InitialPrice-cpt*INCREMENT*Point-SellGoal-spread*Point)/Point;\r
122          }\r
123       }\r
124    }\r
125 //+------------------------------------------------------------------+   \r
127     Comment("mGRID EXPERT ADVISOR ver 2.0\n",\r
128             "FX Acc Server:",AccountServer(),"\n",\r
129             "Date: ",Month(),"-",Day(),"-",Year()," Server Time: ",Hour(),":",Minute(),":",Seconds(),"\n",\r
130             "Minimum Lot Sizing: ",MarketInfo(Symbol(),MODE_MINLOT),"\n",\r
131             "Account Balance:  $",AccountBalance(),"\n",\r
132             "Symbol: ", Symbol(),"\n",\r
133             "Price:  ",NormalizeDouble(Bid,4),"\n",\r
134             "Pip Spread:  ",MarketInfo("EURUSD",MODE_SPREAD),"\n",\r
135             "Increment=" + INCREMENT,"\n",\r
136             "Lots:  ",LOTS,"\n",\r
137             "Levels: " + LEVELS,"\n");\r
138    return(0);\r
141 //+------------------------------------------------------------------+\r
143 int CheckProfits(double LOTS, int Goal, bool Current, double InitialPrice)\r
145    int profit=0, cpt;\r
146    if(Current)//return current profit\r
147    {\r
148       for(cpt=0;cpt<OrdersTotal();cpt++)\r
149       {\r
150          OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);\r
151          if(OrderSymbol()==Symbol() && StrToDouble(OrderComment())==InitialPrice)\r
152          {\r
153             if(OrderType()==OP_BUY) profit+=(Bid-OrderOpenPrice())/Point*OrderLots()/LOTS;\r
154             if(OrderType()==OP_SELL) profit+=(OrderOpenPrice()-Ask)/Point*OrderLots()/LOTS;\r
155          }\r
156       }\r
157       return(profit);\r
158    }\r
159    else\r
160    {\r
161       if(Goal==OP_BUY)\r
162       {\r
163          for(cpt=0;cpt<OrdersTotal();cpt++)\r
164          {\r
165             OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);\r
166             if(OrderSymbol()==Symbol() && StrToDouble(OrderComment())==InitialPrice)\r
167             {\r
168                if(OrderType()==OP_BUY) profit+=(OrderTakeProfit()-OrderOpenPrice())/Point*OrderLots()/LOTS;\r
169                if(OrderType()==OP_SELL) profit-=(OrderStopLoss()-OrderOpenPrice())/Point*OrderLots()/LOTS;\r
170                if(OrderType()==OP_BUYSTOP) profit+=(OrderTakeProfit()-OrderOpenPrice())/Point*OrderLots()/LOTS;\r
171             }\r
172          }\r
173          return(profit);\r
174       }\r
175       else\r
176       {\r
177          for(cpt=0;cpt<OrdersTotal();cpt++)\r
178          {\r
179             OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);\r
180             if(OrderSymbol()==Symbol() && StrToDouble(OrderComment())==InitialPrice)\r
181             {\r
182                if(OrderType()==OP_BUY) profit-=(OrderOpenPrice()-OrderStopLoss())/Point*OrderLots()/LOTS;\r
183                if(OrderType()==OP_SELL) profit+=(OrderOpenPrice()-OrderTakeProfit())/Point*OrderLots()/LOTS;\r
184                if(OrderType()==OP_SELLSTOP) profit+=(OrderOpenPrice()-OrderTakeProfit())/Point*OrderLots()/LOTS;              \r
185             }\r
186          }\r
187          return(profit);\r
188       }\r
189    }\r
192 bool EndSession()\r
194    int cpt, total=OrdersTotal();\r
195    for(cpt=0;cpt<total;cpt++)\r
196    {\r
197       Sleep(3000);\r
198       OrderSelect(cpt,SELECT_BY_POS,MODE_TRADES);\r
199       if(OrderSymbol()==Symbol() && OrderType()>1) OrderDelete(OrderTicket());\r
200       else if(OrderSymbol()==Symbol() && OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,3);\r
201       else if(OrderSymbol()==Symbol() && OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,3);\r
202       \r
203       }\r
204       if(!CONTINUE)  Enter=false;\r
205       return(true);\r
209 double getPipValue(double ord,int dir)\r
211    double val;\r
212    RefreshRates();\r
213    if(dir == 1) val=(NormalizeDouble(ord,Digits) - NormalizeDouble(Ask,Digits));\r
214    else val=(NormalizeDouble(Bid,Digits) - NormalizeDouble(ord,Digits));\r
215    val = val/Point;\r
216    return(val);   \r
219 //========== FUNCTION takeProfit\r
221 void takeProfit(int current_pips, int ticket)\r
223    if(OrderSelect(ticket, SELECT_BY_TICKET))\r
224    {\r
226       if(current_pips >= nextTP && current_pips < (nextTP + Target_Increment))\r
227       {\r
228          if(OrderType()==1)\r
229          {\r
230             if(OrderClose(ticket, MAX_LOTS, Ask, 3))\r
231             nextTP+=Target_Increment;\r
232             else\r
233             Print("Error closing order : ",GetLastError()); \r
234          } \r
235          else\r
236          {\r
237             if(OrderClose(ticket, MAX_LOTS, Bid, 3))\r
238             nextTP+=Target_Increment;\r
239             else\r
240             Print("Error closing order : ",GetLastError()); \r
241          }        \r
242       }\r
243    }\r